package com.starsea.book.ui.main.bookshelf.style1.books

import android.content.Context
import android.text.TextUtils
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.starsea.book.base.adapter.ItemViewHolder
import com.starsea.book.databinding.ItemBookshelfListBinding
import com.starsea.book.help.book.isLocal
import com.starsea.book.ui.newpage.adapter.HomeShelfGroupBookImgAdapter
import com.starsea.book.ui.newpage.entity.BookShelfEntity
import com.starsea.book.ui.newpage.utils.loadNormalImage
import com.starsea.book.utils.gone
import com.starsea.book.utils.visible
import splitties.views.onLongClick

class BooksAdapterList(var mContext: Context, private val callBack: CallBack) :
    BaseBooksAdapter<ItemBookshelfListBinding>(mContext) {

    override fun getViewBinding(parent: ViewGroup): ItemBookshelfListBinding {
        return ItemBookshelfListBinding.inflate(inflater, parent, false)
    }

    override fun convert(
        holder: ItemViewHolder,
        binding: ItemBookshelfListBinding,
        item: BookShelfEntity,
        payloads: MutableList<Any>
    ) = binding.run {
//        val bundle = payloads.getOrNull(0) as? Bundle
//        if (bundle == null) {
        rtvSel.visible(isManage)
        rtvSel.isSelected = item.isSel

        tvRead.text = if (item.isLocal) {
            item.book!!.durChapterTitle
        } else {
            item.sectionTitle
        }
        tvName.text = if (item.isLocal) {
            item.book!!.name
        } else {
            item.bookTitle
        }

        if (item.isLocal) {
            ivCover.load("", "", "", false, "")
        } else {
//            ivCover.load(
//                item.bookCover,
//                item.bookTitle,
//                "", false,
//                ""
//            )
            ivCover.loadNormalImage(mContext, item.bookCover ?: "")
        }

    }


    override fun registerListener(holder: ItemViewHolder, binding: ItemBookshelfListBinding) {

        binding.rtvSel.setOnClickListener {
            getItem(holder.layoutPosition)?.let {
                onClick(it, holder)
            }
        }
        holder.itemView.apply {
            setOnClickListener {
                getItem(holder.layoutPosition)?.let {
                    onClick(it, holder)

                }
            }

            onLongClick {
                getItem(holder.layoutPosition)?.let {
                    if (it.isLocal) {
                        callBack.openBookInfo(it.book!!.bookUrl, holder.layoutPosition)
                    } else {
                        callBack.openBookInfo(it.bookId ?: "", holder.layoutPosition)
                    }
                }
            }
        }
    }

    private fun onClick(
        it: BookShelfEntity,
        holder: ItemViewHolder
    ) {
        if (it.book == null) {
            callBack.open(it.bookId ?: "", holder.layoutPosition)
        } else {
            if (it.isLocal) {
                callBack.openLocal(it.book!!.bookUrl, holder.layoutPosition)
            } else {
                callBack.open(it.bookId ?: "", holder.layoutPosition)
            }
        }
    }
}